<template>
	<view class="wx-createGroup" :style="'height: calc(100vh - '+windowTop+'px)'">
		<watermark></watermark>
		<view class="wx-createGroup-check">
			<uni-indexed-list-wx showSelect :showAdd="false" :options="list" @click="bindClick"></uni-indexed-list-wx>
		</view>
		<view style="height: 60px;"></view>
		<view class="wx-createGroup-foot">
			<view class="wx-btn" @click="createGroup">
				完成
				<text v-if="select.length>0">({{select.length}})</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				select:[],
				windowTop:0
			}
		},
		mounted() {
			this.getflist()
			uni.getSystemInfo({
				success: (res)=> {
					this.windowTop=res.windowTop
				}
			});
		},
		methods: {
			createGroup(){
    			uni.showLoading({
    				title:'创建中'
    			})
				var ids=[]
				for (var i = 0; i < this.select.length; i++) {
					ids.push(this.select[i].data.userId)
				}
                if(ids.length==0){
                    uni.showToast({title:'请选择好友',icon:'none'});return;
                }
                var param = new Object;
                param.ids=ids;
				this.$http.request({
					url: '/group/createGroup',
					method: 'POST',
					data:param,
					success: (res) => {
						if (res.data.code == 200) {
                            var ret = res.data.data;console.log(ret);
                            uni.showToast({
    								title:'创建成功',
    								icon:'none'
    							});
						    if(ret && ret.content){
    							var content = '你邀请'+ret.content+'加入了群聊';
    							this.$fc.pushAlertMsg({
        							msgContent: content, 	//msg内容
        							windowType: 'GROUP', 	//聊天室类型 SINGLE GROUP
        							userId: ret.groupId, 	//ID
    							});
    						}
                            setTimeout(function(){
    							uni.redirectTo({
    								url: this.$fc.get_package_name()+'chatWindow/index?userId=' + ret.groupId + '&windowType=GROUP'
    							})
                            },1000);
						}
					}
				});
			},
			bindClick(e){
				this.select=e.select
			},
			getflist(){
				this.$http.request({
					url: '/friend/friendList',
					method: 'POST',
					data:JSON.stringify({}),
					success: (res) => {
						if (res.data.code == 200) {
							var data=res.data.data.data;
							var list=[]
							for (var i = 0; i < data.length; i++) {
								var item=data[i]
								if(item.userType=='normal'){
									list.push({
										name:item.nickname,
										avatar:item.portrait,
										userId:item.fid,
										chatNo:item.fid
									})
								}
							}
							this.list=this.$fc.sortList({list:list,key:'name'})
						}
					}
				});
			},
		},
	}
</script>

<style scoped>
	.wx-createGroup{
		display: flex;flex-direction: column;
	}
	.wx-createGroup-check{
		height: 90%;
		position: relative;
	}
	.wx-createGroup-foot {
		width: 100%;
		height: 60px;
		line-height: 60px;
		background-color: #eee;
		position: fixed;
		bottom: 0;
		left: 0;
		text-align: right;
	}
	.wx-btn {
		height: 40px;
		line-height: 40px;
		min-width: 200rpx;
		text-align: center;
		border-radius: 12rpx;
		background-color: #05c160;
		color: #fff;
		font-size: 32rpx;
		display: inline-block;
		margin-top: 10px;
		margin-right: 20rpx;
	}
</style>
